package com.gopay.common.trace.def;


import com.gopay.common.trace.enums.TraceType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ddh02
 * @Span
 * @2017/3/30 9:59  www.gopay.com.cn Inc.All rights reserved.
 */


public class SpanBak implements Serializable {
    private String traceId;
    private String id;
    private String parentId;
    private String spanName;
    private String serviceId; // 暂时没用
    private List<Annotation> annotations; //为了方便计算 将单个an 设置成成员变量
    private List<BinaryAnnotation> binaryAnnotations;
    private boolean isSample;

    private List<SpanBak> childs = new ArrayList<>(); //父子关系

    private String traceType = TraceType.RPC.toString();

    @Deprecated
    private Annotation cr;
    @Deprecated
    private Annotation cs;
    @Deprecated
    private Annotation sr;
    @Deprecated
    private Annotation ss;

    private long startTime;  //开始时间 弃用 annotation

    private long endTime;    //结束时间 弃用 annotation

    private long middleTime;  //中间时间

    private Endpoint ep;

    public SpanBak(){
      //  annotations = new ArrayList<Annotation>();
     //   binaryAnnotations = new ArrayList<BinaryAnnotation>();
    }

    public void addAnnotation(Annotation a){
        annotations.add(a);
    }

    public void addBinaryAnnotation(BinaryAnnotation a){
        if (binaryAnnotations == null) {
            binaryAnnotations = new ArrayList<BinaryAnnotation>();
        }
        binaryAnnotations.add(a);
    }

    public Endpoint getEp() {
        return ep;
    }

    public void setEp(Endpoint ep) {
        this.ep = ep;
    }

    public long getMiddleTime() {
        return middleTime;
    }

    public void setMiddleTime(long middleTime) {
        this.middleTime = middleTime;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public String getSpanName() {
        return spanName;
    }

    public void setSpanName(String spanName) {
        this.spanName = spanName;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public List<Annotation> getAnnotations() {
        return annotations;
    }

    public void setAnnotations(List<Annotation> annotations) {
        this.annotations = annotations;
    }

    public List<BinaryAnnotation> getBinaryAnnotations() {
        return binaryAnnotations;
    }

    public void setBinaryAnnotations(List<BinaryAnnotation> binaryAnnotations) {
        this.binaryAnnotations = binaryAnnotations;
    }

    public boolean isSample() {
        return isSample;
    }

    public void setSample(boolean sample) {
        isSample = sample;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getTraceType() {
        return traceType;
    }

    public void setTraceType(String traceType) {
        this.traceType = traceType;
    }

    public List<SpanBak> getChilds() {
        return childs;
    }

    public void setChilds(List<SpanBak> childs) {
        this.childs = childs;
    }

    public void addChild(SpanBak child) {
        child.setParentId(this.getId());
        child.setTraceId(this.getTraceId());
        this.childs.add(child);
    }

    public Annotation getCr() {
        return cr;
    }

    public void setCr(Annotation cr) {
        this.cr = cr;
    }

    public Annotation getCs() {
        return cs;
    }

    public void setCs(Annotation cs) {
        this.cs = cs;
    }

    public Annotation getSr() {
        return sr;
    }

    public void setSr(Annotation sr) {
        this.sr = sr;
    }

    public Annotation getSs() {
        return ss;
    }

    public void setSs(Annotation ss) {
        this.ss = ss;
    }

    @Override
    public String toString() {
        return "Span{" +
                "traceId=" + traceId +
                ", id=" + id +
                ", parentId=" + parentId +
                ", serviceId=" + serviceId +
                ", spanName='" + spanName + '\'' +
                ", annotations=" + annotations +
                ", binaryAnnotations=" + binaryAnnotations +
                ", isSample=" + isSample +
                ", traceType=" + traceType +
                '}';
    }


    public static void main(String[] args) {
        SpanBak s1 = new SpanBak();
        s1.setId("1");

        SpanBak s1_1 = new SpanBak();
        s1_1.setId("1_1");

        SpanBak s1_2 = new SpanBak();
        s1_2.setId("1_2");
        s1.addChild(s1_1);
        s1.addChild(s1_2);


        SpanBak s1_1_1 = new SpanBak();
        s1_1_1.setId("1_1_1");
        s1_1.addChild(s1_1_1);

       // s1.showTree(s1, 0);



    }

}
